﻿// DataSet Object
function dataSetObject() {
	
	// attribute
	this.xmlDoc = "";
	this.dataSetName = "dataset";				// 名稱

	this.tables = new Array();					// DataTable 的物件陣列

	
	// method
	this.getDataSetName = _getDataSetName;
	this.setDataSetName = _setDataSetName;
	this.getTable = _getTable;
	this.getTableCount = _getTableCount;
	this.createTable = _createTable;
	this.load = _load;
	this.toXmlString = _toXmlString;
	
	// implement abstract method
	function _getDataSetName(){
		// 設定 dataset ojbect 的名稱

		return this.dataSetName;
	}
	function _setDataSetName(p_DataSetName) {
		// 取得 dataset ojbect 的名稱

		this.dataSetName = p_DataSetName;
	}
	function _createTable(p_TableName) {
		var i = this.tables.length;
		var tName = "table_" + i;
		if (arguments.length == 1) {
			tName = p_TableName;
		}
		
		this.tables[i] = new dataTableObject(tName);
		return this.tables[i];
	}
	function _getTable(p_TableName){
		// 	
		if (isNaN(p_TableName)) {
			var t_length = this.tables.length;
			for (var i = 0; i < t_length; i++) {
				if (this.tables[i].tableName == p_TableName) {
					return this.tables[i];
				}
			}
			throw "table not found!!";
		}
		else{
			var t_length = this.tables.length;
			if (p_TableName > t_length - 1) {
				// 超過物件的索引範圍

				throw "sub-script out off range!!";
			}
			else{
				return this.tables[p_TableName];
			}
		}
	}
	function _getTableCount(){
		// 取得 table 的數目

		return this.tables.length;
	}
	
	function _load(p_xml) {
		
		//確定是否有錯誤

		var ReturnCode =  p_xml.selectSingleNode("//ReturnCode")
		if(ReturnCode.text == "999"){
			throw "_load() : " + p_xml.text;
		}
		
		// 將資料載入 xml dom
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		try {
			this.xmlDoc = p_xml;
			xmlDoc.loadXML(p_xml.xml);
		}
		catch (e) {
			throw "xml format invalid!!";
		}
		
		//
		var tablelists = xmlDoc.selectNodes("//info");
		var info_table;
		if (tablelists.length <= 0 ) {
			throw "Constructor parameter(xml format) invalid";
		}
		else{
			// 建立 info 的 DataTable Oject
			var info_table = this.createTable("info");
		}
				
		//for (var k = 0; k < 1; k++) {
		for (var k = 0; k < tablelists.length; k++) {
			
			// info DataTable Oject 的 Row 資料
			var info_row = info_table.createRow();
			info_row.setAttribute("TransResult", tablelists[k].selectSingleNode("TransResult").text);
			info_row.setAttribute("ReturnCode", tablelists[k].selectSingleNode("ReturnCode").text);
			info_row.setAttribute("RecordsAffected", tablelists[k].selectSingleNode("RecordsAffected").text);
			info_row.setAttribute("Message", tablelists[k].selectSingleNode("Message").text);
			info_row.setAttribute("RowCount", tablelists[k].selectSingleNode("RowCount").text);
				
			// 判斷交易結果 (查詢)
			if (tablelists[k].selectSingleNode("ReturnCode").text == "000") {
				// 成功取得資料, 建立資料對應的 DataTable Object
				var result_table = this.createTable(tablelists[k].selectSingleNode("TransResult").text);  
				
				
				// 聯繫資料欄標題 (顯示欄位名稱 column name)
				var columnName = tablelists[k].getAttribute("id") + "_Columns";
				var columns = xmlDoc.selectNodes("//" + columnName + "/Column");
				for (var i_col_count = 0; i_col_count < columns.length; i_col_count++) {
					// column name (多國語言碼)
					result_table.setAttribute(columns[i_col_count].getAttribute("id"), columns[i_col_count].getAttribute("name"));
				}
				
				
				// 聯繫資料內容 (欄位值 row value)
				var rowDatas = "//" + tablelists[k].getAttribute("id") + "_Data";
				
				// 資料總筆數 (row count)
				var dataCount = xmlDoc.selectNodes(rowDatas + "/Items").length;
				for (var i = 0; i < dataCount; i++) {
					//加入欄位資料
					var result_row = result_table.createRow();
					var oRowNode = xmlDoc.selectNodes(rowDatas + "//Items").item(i);
					for (var i_col_count = 0; i_col_count < columns.length; i_col_count++) {
						result_row.setAttribute(columns[i_col_count].getAttribute("id"), oRowNode.childNodes.item(i_col_count).text);
					}	
				}
			}
		}
	}
	
	function _toXmlString() {
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.resolveExternals = false;
		
		// 建立根節點 (root)
		var elem_root = xmlDoc.createElement("Response");
		elem_root.setAttribute("id", this.dataSetName)
		xmlDoc.documentElement = elem_root;
		
		// 建立 info 表格
		
		// 建立 table 表格
		
		return xmlDoc.xml;
	}


	/*
	 ****************************************************
	 *	物件名稱 : DataTable Object
	 ****************************************************
	*/
	function dataTableObject(p_tablename) {
	
		if(arguments.length != 1) {
			throw "dataTable Object Constructor parameter invalid";
		}
		
		// attribute
		this.tAttribute = new Object();
		this.tableName = arguments[0];				// 名稱
	
		this.columns = new Array();			// column 的物件陣列

		this.rows = new Array();			// Row 的物件陣列

		
		// method
		this.getTableName = _getTableName;
		this.setTableName = _setTableName;
		this.getAttribute = _getAttribute;
		this.setAttribute = _setAttribute;
		this.getTable = _getTable;
		this.getRowCount = _getRowCount;
		this.createRow = _createRow;
		this.getRow = _getRow;
		
		// implement abstract method
		function _getTableName(){
			// 設定 table ojbect 的名稱

			return this.tableName;
		}
		function _setTableName(p_DataSetName) {
			// 取得 table ojbect 的名稱

			this.dataSetName = tableName;
		}
		function _getAttribute(p_id){
			// 設定節點(table)的屬性

			return this.tAttribute[p_id];
		}
		function _setAttribute(p_id, p_value){
			// 設定節點(table)的屬性

			this.tAttribute[p_id] = p_value;
		}
		
		function _createRow() {
			var i = this.rows.length;			
			this.rows[i] = new dataRowObject();
			return this.rows[i];
		}
		function _getRow(p_index){
			// 	
			if (isNaN(p_index)) {
				var t_length = this.tables.length;
				if (p_index > t_length - 1) {
					// 超過物件的索引範圍

					throw "sub-script out off range!!";
				}
				else{
					return this.rows[p_index];
				}
			}
			else{
				throw "parameter is not number!!";
			}
		}
		function _getRowCount(){
			// 取得 Row 的數目

			return this.rows.length;
		}
		
		
		
		/*
			*******依傳入的值找出第一個符合該值的Row，若沒找到則傳回-1***** 
			傳入參數：column name 及要找尋的值

			***************************************************************
			add by cedric--20050310
		*/
		function _getRow(colunmName,value)
		{
			var rowIndex = -1;
			var compareStr ;
			try
			{
				if (value == null)
					compareStr = "";
				else
					compareStr = value.toString();
					
				for (var i=0;i<this.getRowCount();i++)
				{
					if (this.rows[i].getAttribute(colunmName).toUpperCase() == compareStr.toUpperCase())
					{
						rowIndex = i;
						break;
					}
				}
			}
			catch(e){}
			return rowIndex;
		}
		
		/*
		 ****************************************************
		 *	物件名稱 : DataRow Object
		 ****************************************************
		*/
		function dataRowObject() {
			// attribute
			this.rAttribute = new Object;

			// method
			this.getAttribute = _getAttribute;
			this.setAttribute = _setAttribute;
			
			function _getAttribute(p_id){
				// 設定節點(row)的屬性

				return this.rAttribute[p_id];
			}
			function _setAttribute(p_id, p_value){
				// 設定節點(row)的屬性

				this.rAttribute[p_id] = p_value;
			}
			
		}
		
	}
	
	
}